//
//  MRCityLocalDiscount_TableViewCell.m
//  Travel_World
//
//  Created by dllo on 15/10/16.
//  Copyright © 2015年 TLS. All rights reserved.
//

#import "MRCityLocalDiscount_TableViewCell.h"
#import "MRCityLocalDiscount_CollectionViewCell.h"
#import "MRCountry_DiscountModel.h"
#import "UIImageView+WebCache.h"


@implementation MRCityLocalDiscount_TableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self createCollectionView];
    }
    return self;
}

- (void)createCollectionView
{
    
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.itemSize = CGSizeMake(160 * WID , 190 * HEI);
    flowLayout.sectionInset = UIEdgeInsetsMake(10 * HEI, 10 * WID, 0, 10 * WID);
//    flowLayout.minimumLineSpacing = 20 * WID;
//    flowLayout.minimumInteritemSpacing = 20 * HEI;
//    self.collectionView = [[UICollectionView alloc] init];
    
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, 375 * WID, 400 * HEI) collectionViewLayout:flowLayout];
    self.collectionView.bounces = NO;
    self.collectionView.backgroundColor = [UIColor whiteColor];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    
    [self.contentView addSubview:self.collectionView];
    
    // 注册cell
    [self.collectionView registerClass:[MRCityLocalDiscount_CollectionViewCell class] forCellWithReuseIdentifier:@"reuse"];
    
//    NSLog(@"+)+)+)+)+)())*()*()+)_)+%@",self.arr);
    
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
//    if (self.arr.count != 0) {
        return self.arr.count;
//    }
//    return 0;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    MRCityLocalDiscount_CollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"reuse" forIndexPath:indexPath];
    if (self.arr.count > indexPath.item) {
    
        MRCountry_DiscountModel *model = self.arr[indexPath.row];
        
        [cell.ImageView sd_setImageWithURL:[NSURL URLWithString:model.photo]placeholderImage:[UIImage imageNamed:@"placeholder1"]];
        cell.title.text = model.title;
        
        NSString *priceStr = model.price;
        NSString *price = [priceStr substringWithRange:NSMakeRange(4, priceStr.length-11)];
        cell.price.text = [NSString stringWithFormat:@"%@元起",price];
        
        cell.priceoff.text = model.priceoff;
//        cell.price.text = model.price;
        
        return cell;
    }
    return nil;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"collectionView点击");
    
    NSDictionary *dic = @{@"index":[self.arr[indexPath.row] Id]};
    [[NSNotificationCenter defaultCenter] postNotificationName:@"collectionViewClick" object:nil userInfo:dic];
}

- (void)layoutSubviews
{
    [super layoutSubviews];
//    if (self.arr.count != 0) {
        self.collectionView.frame = CGRectMake(0, 0, self.collectionView.frame.size.width, 400 * HEI);
//    }
    
    
//    NSLog(@"+)+)+)+)+)())*()*()+)_)+%@",self.arr);
    
}

- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
